package com.qhad.ads;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.SyncStateContract.Constants;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.qhad.ads.activity.CustomLandingPageView;
import com.qhad.ads.activity.NativeActivity;
import com.qhad.ads.activity.NativeBannerActivity;
import com.qhad.ads.activity.SampleBannerActivity;
import com.qhad.ads.activity.SampleFloatBannerActivity;
import com.qhad.ads.activity.SampleInterstitialActivity;
import com.qhad.ads.sdk.adcore.Qhad;


public class MainActivity extends Activity {
	private  int REQUEST_CODE = 1;
	private  int clickCount = 1;
	private  Button sampleFloatBannerBtn,sampleBannerBtn,sampleInterstitialBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Qhad.setLogSwitch(this, true);//注释此行关闭LOG
        LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        setContentView(linearLayout, lp);

        this.setTitle("360广告");
        /*
        CheckBox cbCustomLandingPage = new CheckBox(this);
        cbCustomLandingPage.setText("使用自定义落地页浏览器");
        cbCustomLandingPage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (Build.VERSION.SDK_INT > 8) {
                    if (isChecked) {
                        Qhad.setLandingPageView(MainActivity.this, new CustomLandingPageView());
                    } else {
                        Qhad.setLandingPageView(MainActivity.this, null);
                    }
                }
            }
        });
       
        LinearLayout.LayoutParams cblp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.addView(cbCustomLandingPage, cblp);
         */
        Qhad.setLandingPageView(MainActivity.this, new CustomLandingPageView());
        
        LinearLayout.LayoutParams btnlp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        btnlp.topMargin = 10;
        btnlp.gravity = Gravity.CENTER_HORIZONTAL;

         sampleBannerBtn = new Button(this);
        sampleBannerBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, SampleBannerActivity.class);
                startActivityForResult(intent,REQUEST_CODE );
            }
        });
        sampleBannerBtn.setText("条幅广告");
        linearLayout.addView(sampleBannerBtn, btnlp);

         sampleFloatBannerBtn = new Button(this);
        sampleFloatBannerBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, SampleFloatBannerActivity.class);
                startActivityForResult(intent,REQUEST_CODE );
            }
        });
        sampleFloatBannerBtn.setText("浮动条幅广告");
        linearLayout.addView(sampleFloatBannerBtn, btnlp);

         sampleInterstitialBtn = new Button(this);
        sampleInterstitialBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, SampleInterstitialActivity.class);
                startActivityForResult(intent,REQUEST_CODE );
            }
        });
        sampleInterstitialBtn.setText("插屏广告");
        linearLayout.addView(sampleInterstitialBtn, btnlp);
        /*
        Button nativeBtn = new Button(this);
        nativeBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, NativeActivity.class);
                startActivity(intent);
            }
        });
        nativeBtn.setText("原生广告");
        linearLayout.addView(nativeBtn, btnlp);
        */
        /*
        Button SplashBtn = new Button(this);
        SplashBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, SplashActivity.class);
                startActivity(intent);
            }
        });
        SplashBtn.setText("开屏广告");
        linearLayout.addView(SplashBtn, btnlp);
		*/
        /*
        Button nativeBannerBtn = new Button(this);
        nativeBannerBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(MainActivity.this, NativeBannerActivity.class);
                startActivity(intent);
            }
        });
        nativeBannerBtn.setText("单品广告");
        linearLayout.addView(nativeBannerBtn, btnlp);
        */
        timer = new Timer();
        task = new TimerTask(){  
      	   
            public void run() {  
                Message message = new Message();      
                message.what = clickCount;     
                handler.sendMessage(message);
            }  
               
        }; 
        timer.schedule(task, 1000,1000);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if(requestCode==REQUEST_CODE){
    		clickCount = resultCode;
    		if(++clickCount > 3){
    			clickCount = 1;
    		}
    		clickButton();
    	}
    }
    private void clickButton(){
    	timer = new Timer();
    	task = new TimerTask(){  
     	   
            public void run() {  
                Message message = new Message();      
                message.what = clickCount;     
                handler.sendMessage(message);
            }  
               
        }; 
    	timer.schedule(task, 1000,1000);
    }
    
    private Timer timer;

    Handler handler = new Handler(){  
    	   
        public void handleMessage(Message msg) {  
            switch (msg.what) { 
            case 1:
            	timer.cancel();
            	task.cancel();
            	task = null;
            	timer = null;
            	sampleBannerBtn.performClick();
            	break;
            case 2:      
            	timer.cancel();
            	task.cancel();
            	task = null;
            	timer = null;
            	sampleFloatBannerBtn.performClick();
                break;      
            case 3:
            	timer.cancel();
            	task.cancel();
            	task =null;
            	timer = null;
            	sampleInterstitialBtn.performClick();
            	break;
            }      
            super.handleMessage(msg);  
        }  
    };
    
    TimerTask task;
}
